package com.itheima.pinda.web;

import com.itheima.j2cache.anno.Cache;
import com.itheima.j2cache.anno.CacheEvictor;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.AddressBook;
import com.itheima.pinda.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.oschina.j2cache.CacheChannel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * 地址簿管理
 *
 * @author llhuang10
 * @date 2021/10/7 15:26
 */
@Slf4j
@Validated
@Api(tags = "地址簿管理")
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    private static final String REGION = "addressBook";

    @Resource
    private AddressBookService addressBookService;
    @Resource
    private CacheChannel cacheChannel;


    /**
     * 保存地址簿
     *
     * @param entity 地址薄
     * @return 返回结果
     */
    @PostMapping
    @ApiOperation("保存地址簿")
    public Result save(@Valid @RequestBody AddressBook entity) {
        return addressBookService.saveAddressBook(entity);
    }


    /**
     * 根据Id查询地址簿详情
     *
     * @param id 地址薄Id
     * @return 地址薄
     */
    @GetMapping("/detail/{id}")
    @ApiOperation("根据Id查询地址簿详情")
    @Cache(region = REGION, key = "ab", params = "id")
    public AddressBook detail(@NotBlank(message = "地址薄Id不能空") @PathVariable("id") String id) {
        return addressBookService.detail(id);
    }

    /**
     * 分页查询
     *
     * @param page     当前页
     * @param pageSize 页码大小
     * @param userId   用户Id
     * @param keyword  关键字 姓名 | 手机号 | 公司名称
     * @return 分页返回参数
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public PageResponse<AddressBook> page(@RequestParam(name = "page") Integer page,
                                          @RequestParam(name = "pageSize") Integer pageSize,
                                          @RequestParam(name = "userId", required = false) String userId,
                                          @RequestParam(name = "keyword", required = false) String keyword) {
        return addressBookService.pageAddressBook(page, pageSize, userId, keyword);
    }

    /**
     * 修改地址薄
     *
     * @param id     地址薄Id
     * @param entity 地址信息
     * @return 返回结果
     */
    @PutMapping("/{id}")
    @ApiOperation("修改地址薄")
    @CacheEvictor(@Cache(region = REGION, key = "ab", params = "1.id")) // 修改需要清理缓存, 1.id 明确指定param的参数
    public Result update(@NotBlank(message = "地址薄Id不能空") @PathVariable("id") String id,
                         @Valid @RequestBody AddressBook entity) {
        return addressBookService.updateAddressBook(id, entity);
    }

    /**
     * 删除地址薄
     *
     * @param id 地址薄Id
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除地址薄")
    @CacheEvictor(@Cache(region = REGION, key = "ab", params = "id")) // 删除也需要清理缓存
    public Result del(@NotBlank(message = "地址薄Id不能空") @PathVariable("id") String id) {
        return addressBookService.delAddressBook(id);
    }
}
